#include "screen.h"
#include <iostream>
#include "display_bmp.h"
#include "system_orders.h"
#include "font.h"

using namespace std;
System_orders::System_orders()
{
    counts = 0;
    flag = 0;
    interface_page = 1;
    Menutotalprice = 0;
    M = NULL;
}




void System_orders::Update_list_data()
{

    myMenu.push_back(new Menu((char *)"mun1", (char *)"./bmp/11.bmp",24));
    myMenu.push_back(new Menu((char *)"mun2", (char *)"./bmp/12.bmp", 34));
    myMenu.push_back(new Menu((char *)"mnu3", (char *)"./bmp/13.bmp", 54));
    myMenu.push_back(new Menu((char *)"mun4", (char *)"./bmp/14.bmp", 64));
    myMenu.push_back(new Menu((char *)"mun5", (char *)"./bmp/15.bmp", 84));
    myMenu.push_back(new Menu((char *)"mun6", (char *)"./bmp/16.bmp", 64));
    myMenu.push_back(new Menu((char *)"mun7", (char *)"./bmp/17.bmp", 64));
    myMenu.push_back(new Menu((char *)"mun8", (char *)"./bmp/18.bmp", 84));
    myMenu.push_back(new Menu((char *)"mun9", (char *)"./bmp/19.bmp", 24));
    myMenu.push_back(new Menu((char *)"mun10", (char *)"./bmp/20.bmp", 34));
    myMenu.push_back(new Menu((char *)"mun11", (char *)"./bmp/21.bmp", 74));
    myMenu.push_back(new Menu((char *)"mun12", (char *)"./bmp/22.bmp", 88));
}
void System_orders::Update_BMP_data()
{
    int i, j;
    for (unsigned int i = (interface_page - 1) * 8, j = 0; i < myMenu.size() && i < interface_page * 8; i++, j++)
    {
        display_picture_for_bmp_format(j % 4 * 200, j / 4 * 215 + 80, myMenu[i]->bmp_path, 1);
        char buffer[15];
        sprintf(buffer, "$%d", myMenu[i]->prices);
        displayfont(j % 4 * 200+50, j / 4 * 215 + 215, buffer, 50, 255, 255, 255);
    }
}


void System_orders::touchscreen_slide_handler(int start_x, int start_y, int end_x, int end_y)
{
    
    if (abs(end_x - start_x) < 30 && abs(end_y - start_y) < 30)
    {
        if (flag == Start_page)
        {
            if (end_x > 92 && end_x < 333 && end_y > 250 && end_y < 400)
            {
                display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                display_picture_for_bmp_format(700, 480, "./bmp/car.bmp", 1);
                displayfont(400, 20, (char *)"欢迎光临", 200, 255, 255, 255);
                Update_BMP_data();
                cout<<"Update_list_data="<<myMenu.size()<<endl;
                flag = Maininterface;
            }
        }
        else if (flag == Maininterface)
        {
            if (end_y > 60)
            {
                int x = (end_y - 60) / 210 * 4 + end_x / 200 + (interface_page - 1) * 8;
                if (x >= 0 && x < myMenu.size())
                {
                    display_picture_for_bmp_format(160, 60, "./bmp/5.bmp", 1);
                    display_picture_for_bmp_format(300, 80, myMenu[x]->bmp_path, 1);
                    display_picture_for_bmp_format(160, 60, "./bmp/back.bmp", 1);
                    display_picture_for_bmp_format(600, 60, "./bmp/buy.bmp", 1);
                    display_picture_for_bmp_format(450, 260, "./bmp/add.bmp", 1);
                    display_picture_for_bmp_format(290, 260, "./bmp/reduce.bmp", 1);
                    char buffer[15];
                    sprintf(buffer, "$%d", myMenu[x]->prices);
                    displayfont(300, 80, buffer, 50, 255, 255, 255);
                    M = myMenu[x];
                    flag = Addmenutoshopcar;
                }
                return;
            }
            else if (end_x > 770 && end_x < 800 && end_y > 0 && end_y < 40)
            {
                display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                displayfont(70, 20, (char *)"菜品", 200, 255, 255, 255);
                displayfont(300, 20, (char *)"价格", 200, 255, 255, 255);
                displayfont(500, 20, (char *)"份量", 200, 255, 255, 255);
                display_picture_for_bmp_format(700, 400, "./bmp/pay.bmp", 1);
                flag = Shopcarinterface;
                Menutotalprice = 0;
                for (unsigned int i = 0; i < Menushoppingcar.size(); i++)
                {
                    display_picture_for_bmp_format(700, 400, "./bmp/pay.bmp", 1);
                    display_picture_for_bmp_format(20, 400, "./bmp/6.bmp", 1);
                    char buffer[64];
                    displayfont(70, 20, (char *)"名字", 200, 255, 255, 255);
                    displayfont(300, 20, (char *)"价格", 200, 255, 255, 255);
                    displayfont(500, 20, (char *)"份量", 200, 255, 255, 255);
                    sprintf(buffer, "$%d", Menushoppingcar[i]->prices);
                    displayfont(320, 80 + i * 40, buffer, 200, 255, 255, 255);
                    memset(buffer,0, sizeof(buffer));
                    sprintf(buffer, "%s", Menushoppingcar[i]->names);
                    displayfont(70, 80 + i * 40, buffer, 220, 255, 255, 255);
                    memset(buffer, 0,sizeof(buffer));
                    sprintf(buffer, "%d", Menushoppingcar[i]->counts);
                    displayfont(520, 80 + i * 40, buffer, 150, 255, 255, 255);

                    Menutotalprice += Menushoppingcar[i]->prices * Menushoppingcar[i]->counts;

                    flag = Shopcarinterface;
                }
            }
        }
        else if (flag = Addmenutoshopcar)
        {
            if (end_x > 180 && end_x < 210 && end_y > 80 && end_y < 130)
            {
                M = NULL;
                display_picture_for_bmp_format(160, 60, "./bmp/5.bmp", 1);
                Update_BMP_data();
                counts = 0;
                flag = Maininterface;
                return;
            }
            else if (end_x > 450 && end_x < 510 && end_y > 280 && end_y < 310) //加
            {
                counts++;
                char buffer[10];
                sprintf(buffer, "%d", counts);
                displayfont(390, 280, buffer, 50, 255, 255, 255);
                return;
            }
            else if (end_x > 295 && end_x < 350 && end_y > 280 && end_y < 310) //减
            {
                if (counts > 0)
                {
                    counts--;
                }
                char buffer[10];
                sprintf(buffer, "%d", counts);
                displayfont(390, 280, buffer, 50, 255, 255, 255);
                return;
            }
            else if (end_x > 620 && end_x < 680 && end_y > 50 && end_y < 150)
            {
                if (counts > 0)
                {
                    bool Same_product = false;
                    for (unsigned int i = 0; i < Menushoppingcar.size(); i++)
                    {
                        if (strcmp(Menushoppingcar[i]->names, M->names) == 0)
                        {
                            Menushoppingcar[i]->counts += counts;
                            Same_product = true;
                            break;
                        }
                    }
                    if (!Same_product)
                    {
                        Menushoppingcar.push_back(new Menu(M->names, M->bmp_path, M->prices, counts));
                    }
                }
                display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                display_picture_for_bmp_format(740, 0, "./bmp/car.bmp", 1);
                displayfont(300, 20, (char *)"欢迎光临", 200, 255, 255, 255);

                Update_BMP_data();
                M = NULL;
                counts = 0;
                flag = Maininterface;
                return;
            }
            else if (end_x > 70 && end_x < 194 && end_y > 308 && end_y < 480)
            {
                display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                display_picture_for_bmp_format(740, 0, "./bmp/car.bmp", 1);
                displayfont(300, 20, (char *)"欢迎光临", 200, 255, 255, 255);

                Update_BMP_data();
                M = NULL;
                counts = 0;
                flag = Maininterface;
                Menutotalprice = 0;
            }
            else if (end_x > 735 && end_x < 755 && end_y > 420 && end_y < 480) //支付界面
            {
                flag = Paymentinterface;
                display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                display_picture_for_bmp_format(0, 0, "./bmp/33.bmp", 1);

               // displayfont(700, 400, (char *)"退出:", 60, 255, 255, 255);
                //displayfont(20, 400, (char *)"返回:", 60, 255, 255, 255);
                //display_picture_for_bmp_format(20, 400, "./bmp/6.bmp", 1);
                //display_picture_for_bmp_format(20,200,"./bmp/back.bmp",1);

                displayfont(300, 360, (char *)"花费:", 180, 255, 255, 255);
                char buffer[64];
                sprintf(buffer, "%d", Menutotalprice);
                displayfont(430, 360, buffer, 100, 255, 255, 255);
               int K = Menushoppingcar.size();
               send(getsockfd(), &K, sizeof(unsigned int), 0);
               for (unsigned i = 0; i < Menushoppingcar.size(); i++)
                   {
                    cout<<"------------"<<endl;
                     send(getsockfd(), Menushoppingcar[i], sizeof(*Menushoppingcar[i]), 0);

                   }
                     Menushoppingcar.clear();
            }
            else if(end_x > 580 && end_x < 712 && end_y >308 && end_y < 480)
            {
               display_picture_for_bmp_format(0, 0, "./bmp/8.bmp", 1);


               interface_page=1;
               flag=Start_page;
            }
            
        }

        return;
    }

    else //滑动
    {
        if (abs(end_x - start_x) > abs(end_y - start_y))
        {
            if (end_x > start_x) //左右滑动
            {
                if ((interface_page - 1) > 0)
                {
                    interface_page--;
                    display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                    display_picture_for_bmp_format(740, 0, "./bmp/car.bmp", 1);
                    displayfont(300, 20, (char *)"欢迎光临", 200, 255, 255, 255);

                    Update_BMP_data();
                }
            }
            else
            {
                if (interface_page + 1 <= myMenu.size() / 8 + 1)
                {
                    interface_page++;
                    display_picture_for_bmp_format(0, 0, "./bmp/3.bmp", 1);
                    display_picture_for_bmp_format(740, 0, "./bmp/car.bmp", 1);
                    displayfont(300, 20, (char *)"欢迎光临", 200, 255, 255, 255);

                    Update_BMP_data();
                }
            }
        }
    }
}

void System_orders::run()
{
    int x, y;
    int start_x, start_y;
    int stauts;

    display_picture_for_bmp_format(0, 0, "./bmp/8.bmp", 1);
    Update_list_data();
    Connect_the_Server();
    while (1)
    {

        stauts = get_touch_status(&x, &y);
        switch (stauts)
        {
        case SCREEN_COORDINATE:
            //printf("x=%d,y=%d", x, y);
            break;
        case SCREEN_TOUCH:
            start_x = x, start_y = y;
           // printf("x=%d,y=%d\n", x, y);
            //printf("触摸屏被按下了\n");
            start_x = x;
            start_y = y;

            break;
        case SCREEN_RELEASE:
            //printf("x=%d,y=%d\n", x, y);
            //printf("触摸屏松开了\n");

            touchscreen_slide_handler(start_x, start_y, x, y);
            break;
        default:
            break;
        }
    }
}

void System_orders::printT()
{
    for (std::vector<Menu *>::iterator it = Menushoppingcar.begin(); it != Menushoppingcar.end(); it++)
    {
        cout << (*it)->names << " " << (*it)->counts << endl;
    }
}
